package com.fx.device.service.device.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fx.device.common.util.ErrorEnum;
import com.fx.device.common.util.ReturnMessage;
import com.fx.device.common.util.ReturnMessageUtils;
import com.fx.device.repository.DeviceMaintenanceRepository;
import com.fx.device.request.device.*;
import com.fx.device.service.device.DeviceMaintenanceService;
import com.fx.device.vo.device.DeviceMaintenanceRecord;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DeviceMaintenanceServiceImpl implements DeviceMaintenanceService {

    final private DeviceMaintenanceRepository deviceMaintenanceRepository;


    @Override
    public ReturnMessage<Object> add(DeviceMaintenanceAddRequest request) {
        DeviceMaintenanceRecord deviceMaintenanceRecord = new DeviceMaintenanceRecord();
        BeanUtils.copyProperties(request, deviceMaintenanceRecord);
        Integer save = deviceMaintenanceRepository.insert(deviceMaintenanceRecord);
        return ReturnMessageUtils.success(save);
    }

    @Override
    public ReturnMessage<Object> update(DeviceMaintenanceUpdateRequest request) {

        DeviceMaintenanceRecord deviceMaintenanceRecord = new DeviceMaintenanceRecord();
        BeanUtils.copyProperties(request, deviceMaintenanceRecord);
        deviceMaintenanceRepository.updateById(deviceMaintenanceRecord);
        return ReturnMessageUtils.success();
    }

    @Override
    public ReturnMessage<Object> delete(DeviceMaintenanceDelRequest request) {


        if (request.getIds().size() > 0 && request.getIds() != null) {
            if (ObjectUtils.isNotEmpty(deviceMaintenanceRepository.selectBatchIds(request.getIds()))) ;
            {
                deviceMaintenanceRepository.deleteBatchIds(request.getIds());
                return ReturnMessageUtils.success();
            }


        } else {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);

        }
    }

    @Override
    public ReturnMessage<DeviceMaintenanceRecord> getById(DeviceMaintenanceGetByIdRequest request) {
        DeviceMaintenanceRecord deviceMaintenanceRecord = deviceMaintenanceRepository.selectById(request.getId());
        if (ObjectUtils.isEmpty(deviceMaintenanceRecord)) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_RECORD_NOT_FOUND);
        }
        return ReturnMessageUtils.success(deviceMaintenanceRecord);
    }

    @Override
    public ReturnMessage<IPage<DeviceMaintenanceRecord>> query(DeviceMaintenanceQueryRequest request) {


        QueryWrapper<DeviceMaintenanceRecord> queryWrapper = new QueryWrapper();

        queryWrapper.likeRight(request.getMaintenancePlanNo() != null, "maintenancePlanNo", request.getMaintenancePerson()).or()
                .likeRight(request.getMaintenanceTime() != null, "maintenanceTime", request.getMaintenanceTime())
                .or().likeRight(request.getMaintenanceResult() != null, "maintenanceResult", request.getMaintenanceResult()).or()
                .likeRight(request.getMaintenancePerson() != null, "maintenancePerson", request.getMaintenancePlanNo()).orderByDesc("id");
        Integer count = deviceMaintenanceRepository.selectCount(queryWrapper);

        IPage<DeviceMaintenanceRecord> page = new Page<>(request.getPageNo(), request.getPageSize(), count);

        IPage<DeviceMaintenanceRecord> deviceMaintenanceRecordIPage = deviceMaintenanceRepository.selectPage(page, queryWrapper);
        return ReturnMessageUtils.success(deviceMaintenanceRecordIPage);

    }


}


